package com.followmeandroid.models;

/**
 * <p>Title: Token</p>
 * <p>Description: A token to be used in authenticating
 * with the server.</p>
 * @author Adam Drews
 * @email adam.drews@my.uwrf.edu
 * @date 11/11/2014
 * @team Tyler Johnson
 * @email tyler.johnson@my.uwrf.edu
 * @team Nicholas Thorp
 * @email nicholas.thorp@my.uwrf.edu
 */
import com.google.gson.annotations.SerializedName;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.UUID;

public class Token {
    private String access_token;
    private String token_type;
    private UUID memberId;
    private int expires_in;
    private String userName;
    @SerializedName(".issued")
    private Date issued;
    @SerializedName(".expires")
    private Date expires;

    /**
     * Checks the current token to verify its validity.
     * @return  true        The token is good.
     *          false       The token is bad.
     */
    public boolean getValid() {
        Calendar calendar = new GregorianCalendar();
        Calendar ci = Calendar.getInstance();

        String cuDateTime = "" + ci.get(Calendar.YEAR) + "-" +
                (ci.get(Calendar.MONTH) + 1) + "-" +
                ci.get(Calendar.DAY_OF_MONTH) + " " +
                ci.get(Calendar.HOUR) + ":" +
                ci.get(Calendar.MINUTE);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm");
        try {
            Date dateNow = sdf.parse(cuDateTime);
            if (userName != null && token_type != null && access_token != null && expires != null && dateNow.compareTo(expires) < 0)
                return true;
            else
                return false;
        } catch (Exception e) {
        }
        return false;
    }

    /**
     * Returns an access token value.
     * @return access_token     The access token.
     */
    public String getAccess_token() {
        return access_token;
    }

    /**
     * Gets the token type for the current token.
     * @return token_type       The token's type.
     */
    public String getToken_type() {
        return token_type;
    }

    /**
     * Returns the token's expiration value.
     * @return expires_in
     */
    public int getExpires_in() {
        return expires_in;
    }

    /**
     * Returns the user name associated with this token.
     * @return userName     The token's user's name.
     */
    public String getUserName() {
        return userName;
    }

    /**
     * Returns the date the token was issued.
     * @return issued       The tokens date of issue.
     */
    public Date getIssued() {
        return issued;
    }

    /**
     * Returns the expiration date of the token.
     * @return expires      The token's expiration date.
     */
    public Date getExpires() {
        return expires;
    }

    /**
     * Returns the member ID associated with the token.
     * @return memberId     The token's member's ID.
     */
    public UUID getMemberId() {
        return memberId;
    }
}
